﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using outREACH.App_Code;
using System.Web.Security;
using System.Data;
using System.Drawing;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;

namespace outREACH
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.TextBox CodeNumberTextBox;
        protected System.Web.UI.WebControls.Button SubmitButton;
        protected System.Web.UI.WebControls.Label MessageLabel;

        // For generating random numbers.
        private Random random = new Random();

        protected void Page_Load(object sender, EventArgs e)
        {
            Button4.OnClientClick = "return popup('Terms_and_Conditions.aspx', 'Terms_and_Conditions')";
            if (!this.IsPostBack)
            {
                this.Session["CaptchaImageText"] = GenerateRandomCode();
            }
            List<Countries> results = Countries.ListAll();

            foreach (Countries u in results)
            {
                DropDownList4.Items.Add(u.Country);
            }
        }

        private string GenerateRandomCode()
        {
            string s = "";
            for (int i = 0; i < 6; i++)
                s = String.Concat(s, this.random.Next(10).ToString());
            return s;
        }

        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Users u = new Users();
            u.firstName = TextBox2.Text;
            u.lastName = TextBox3.Text;
            u.userName = TextBox4.Text;
            u.password = TextBox6.Text;
            u.gender = DropDownList3.SelectedValue.ToString();
            u.dob = Convert.ToDateTime(TextBox14.Text);
            u.hp = TextBox8.Text;
            u.home = TextBox9.Text;
            u.email = TextBox10.Text;
            u.country = DropDownList4.SelectedValue.ToString();
            u.city = TextBox15.Text;
            u.address = TextBox12.Text;
            u.zipCode = TextBox13.Text;
            string r = "member";
            u.role = r;
            string i = "avatar.png";
            u.imageName = i;
            u.imagePath = "~/Images/" + i;
            u.dateJoin = DateTime.Today;
            List<Users> results = Users.GetUser();
            foreach (Users u1 in results)
            {
                if (TextBox4.Text.ToString() == u1.userName.ToString())
                {
                    Label2.Visible = true;
                    break;
                }
                else
                {
                    if (this.CodeNumberTextBox2.Text == this.Session["CaptchaImageText"].ToString())
                    {
                        Users.Insert(u);
                        break;
                    }
                    else
                    {
                        this.MessageLabel.CssClass = "error";
                        this.MessageLabel.Text = "You have enter the wrong code. Please try again!";
                        this.CodeNumberTextBox2.Text = "";
                        this.Session["CaptchaImageText"] = GenerateRandomCode();
                    }
                }
            }

            Response.Redirect("RedirectMem.aspx");
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {

        }

        protected void TextBox16_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            this.CodeNumberTextBox.Text = "";
            this.Session["CaptchaImageText"] = GenerateRandomCode();
        }


        protected void Button5_Click(object sender, EventArgs e)
        {
            List<Users> results = Users.GetUser();
            foreach (Users u in results)
            {
                if (TextBox4.Text.ToString() == u.userName.ToString())
                {
                    Label2.Visible = true;
                    break;
                }
                else
                {
                    Label3.Visible = true;
                }
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            if (this.CodeNumberTextBox.Text != null)
            {
                this.CodeNumberTextBox.Text = "";
            }
            this.Session["CaptchaImageText"] = GenerateRandomCode();
        }

        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {

        }

      
    }
}